package com.honeyms.project.im.socket.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honeyms.common.utils.R;
import com.honeyms.common.utils.security.ShiroUtils;
import com.honeyms.project.im.socket.domain.Message;
import com.honeyms.project.system.usernotice.domain.UserNotice;

/**
 * 测试系统内通知消息实时推送
 * @author honey
 * @version 创建时间：2020年12月4日 上午10:43:21
 */

@RequestMapping("/chat")
@Controller
public class MessageController {

	/** 消息发送工具对象 */
    @Autowired(required=false)
    private SimpMessageSendingOperations simpMessageSendingOperations;
    
    @GetMapping("/socketpage")
    public String socket() {
    	return "/im/socket/socket";
    }
    
    /**
     * 进入聊天室
     * @return
     */
    @GetMapping("/chatroom")
    public String chatroom() {
    	return "/im/socket/chatroom";
    }
    
    /**
     * 进入聊天室
     * @return
     */
    @GetMapping("/honeychat")
    public String honeychat(ModelMap mmap) {
    	if (ShiroUtils.getSysUser()!=null) {
			mmap.put("user",ShiroUtils.getSysUser());
		}
    	return "/im/socket/honeychat";
    }
    
    @GetMapping("/sendmsg")
    @ResponseBody
    public R testsocket() {
    	UserNotice userNotice = new UserNotice();
    	userNotice.setNoticeTitle("这是一点管理员发送的实时通知!!!");
    	userNotice.setNoticeId(1l);
    	simpMessageSendingOperations.convertAndSend("/topic/notice", userNotice);
    	return R.ok("发送成功");
    }
    
    @PostMapping("/sendmsg")
    @ResponseBody
    public R testsocket1(Message msg) {
    	msg.setUserName(ShiroUtils.getLoginName());
    	msg.setSendTime(new Date());
    	System.out.println(msg);
    	simpMessageSendingOperations.convertAndSend(msg.getSendUrl(), msg);
    	return R.ok("发送成功");
    }
    
}
